﻿using LSL.SqlClient;
using System;
using Liu_Project.Utils;

namespace Liu_Project.Web.Admin.Handler.BaseHand
{
    /// <summary>
    /// UploadHand 的摘要说明
    /// </summary>
    public class UploadHand : HandlerBaseAsync
    {
        public void UploadImg()
        {
            _logOperation = "用户管理-上传用户头像";

            var base64Str = DataConvert.ToString(_context.Request["base64Str"]);
            if (string.IsNullOrEmpty(base64Str))
                throw new Exception("上传的数据不能为空.");

            var uploadPath = _context.Server.MapPath("~/Upload/BackUser/");                                 
            var savePath = System.IO.Path.Combine(uploadPath);

            var fileName = ImageHelper.Base64StrToImg(base64Str, savePath);
            var filePath = "/Upload/BackUser/" + fileName;

            _context.Response.Write(CommonFunction.SuccessJson(filePath));
        }

        public void UploadUserImg()
        {
            _logOperation = "用户管理-上传用户头像";
            var base64Str = DataConvert.ToString(_context.Request["base64Str"]);
            if (string.IsNullOrEmpty(base64Str))
                throw new Exception("上传的数据不能为空.");

            var uploadPath = _context.Server.MapPath("~/Upload/BackUser/");                                    
            var savePath = System.IO.Path.Combine(uploadPath);

            byte[] arr = Convert.FromBase64String(ImageHelper.ReplaceBase64(base64Str));
            System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(arr);
            
            var userImgName = "User_" + CurrentUser.UserId + "_" + CurrentUser.UserNo + ".jpg";
            var fileName = savePath + userImgName;
            using (memoryStream)
            {
                ImageHelper.CutForSquare(memoryStream, fileName, 100, 100);
            }
            var imgUrl = "/Upload/BackUser/" + userImgName;
            _context.Response.Write(CommonFunction.SuccessJson(imgUrl));
        }
    }
}